﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

using EHTWebsite.Code;

/// <summary>
/// Author: toanvt
/// CreatedDate: 10/12/2011
/// Description: Quản trị Thêm, Sửa File
/// </summary>
public partial class Administrator_AdminFileIU : System.Web.UI.Page
{
    public Int32 fileID = -1;
    private String message = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["Message"] != null)
            {
                try
                {
                    message = Request.QueryString["Message"].ToString();
                }
                catch (Exception)
                {
                    message = "";
                }
            }

            //hiển thị Message nếu có giá trị Message gửi sang từ màn hình nguồn
            this.lbMessage.Text = message;
            this.lbMessage.CssClass = "BlueCorlor";

            try
            {
                fileID = Int32.Parse(Request.QueryString["FileID"].ToString());
            }
            catch
            {
                fileID = -1;
            }

            if (fileID == -1)
            {
                this.lbAdminFileIU.Text = "Thêm mới File";
                ViewState["action"] = HangSo.ACTION_ADD;
            }
            else
            {
                this.lbAdminFileIU.Text = "Sửa File";
                EHTWebsite.Code.File theFile = FileProvider.GetFileByFileID(fileID);

                if (theFile != null)
                {
                    this.fckMoTaOther.Value = theFile.MoTaOther;
                    this.fckMoTaVN.Value = theFile.MoTaVN;
                    this.txtFileUrl.Text = theFile.FileUrl;
                    this.txtNumberOfOrder.Text = theFile.NumberOfOrder.ToString();
                    this.ckPublish.Checked = theFile.Publish;
                    ViewState["FileID"] = theFile.FileID;
                    ViewState["action"] = HangSo.ACTION_UPDATE;
                }
            }
        }
    }

    /// <summary>
    /// Author: toanvt
    /// CreatedDate: 10/12/2011
    /// Description: Validate dữ liệu nhập
    /// </summary>
    /// <returns>Trạng thái Validate</returns>
    private Boolean ValidateData()
    {
        this.lbMessage.Text = "";
        this.lbMessage.CssClass = "";

        String message = "";
        Boolean isSuccess = true;
        string sExtension = ".doc,.docx,.pdf,.xlsx,.xls,.txt,.DOC,.DOCX,.XLS,.XLSX,.TXT,.PDF";

        try
        {
            if (ViewState["action"].ToString() == HangSo.ACTION_ADD)
            {
                if (!this.fuFileUrl.HasFile)
                {
                    isSuccess = false;
                    message = "Hãy chọn file.";
                }
                else
                {
                    if (sExtension.IndexOf(Path.GetExtension(fuFileUrl.PostedFile.FileName)) < 0)
                    {
                        isSuccess = false;
                        message = "File không đúng.";
                    }

                }
            }
            else
            {
                if (this.fuFileUrl.HasFile && (sExtension.IndexOf(Path.GetExtension(fuFileUrl.PostedFile.FileName)) < 0))
                {
                    isSuccess = false;
                    message = "File không đúng.";
                }
            }

        }
        catch (ApplicationException ex)
        {
            throw new ApplicationException(ex.Message);
         
        }

        if (!isSuccess)
            lbMessage.Text = message;

        return isSuccess;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Page.Validate();

        if (Page.IsValid)
        {
            if (!ValidateData())
                return;

            Int32 fileIDOut = -1;
            EHTWebsite.Code.File theFile = new EHTWebsite.Code.File();
            theFile.MoTaOther = this.fckMoTaOther.Value;
            theFile.MoTaVN = this.fckMoTaVN.Value;
            theFile.Publish = this.ckPublish.Checked;

            try
            {
                theFile.NumberOfOrder = Int32.Parse(this.txtNumberOfOrder.Text.Trim());
            }
            catch (Exception ex)
            {
                theFile.NumberOfOrder = 1;
            }
           
            if (this.fuFileUrl.HasFile)
            {
                String fileName = fuFileUrl.PostedFile.FileName;
                String fileType = Path.GetExtension(fileName);
                String newFileName = DateTime.Now.Ticks.ToString() + fileType;
                fuFileUrl.PostedFile.SaveAs(Server.MapPath("~/") + "//ImageDatas//CompanyImages//" + newFileName);
                theFile.FileUrl = newFileName;
            }
            else
            {
                theFile.FileUrl = this.txtFileUrl.Text.Trim();
            }

            String action = ViewState["action"].ToString();

            if (action == HangSo.ACTION_ADD)
            {
                theFile.FileID = -1;
                fileIDOut = FileProvider.IUFile(theFile, true);
            }
            else
            {
                theFile.FileID = Int32.Parse(ViewState["FileID"].ToString());
                fileIDOut = FileProvider.IUFile(theFile, false);
            }

            Response.Redirect("AdminFiles.aspx");
        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("AdminFiles.aspx");
    }

    protected void btnApply_Click(object sender, EventArgs e)
    {
        Page.Validate();

        if (Page.IsValid)
        {
            if (!ValidateData())
                return;

            Int32 fileIDOut = -1;
            EHTWebsite.Code.File theFile = new EHTWebsite.Code.File();
            theFile.MoTaOther = this.fckMoTaOther.Value;
            theFile.MoTaVN = this.fckMoTaVN.Value;
            theFile.Publish = this.ckPublish.Checked;

            try
            {
                theFile.NumberOfOrder = Int32.Parse(this.txtNumberOfOrder.Text.Trim());
            }
            catch (Exception ex)
            {
                theFile.NumberOfOrder = 1;
            }

            if (this.fuFileUrl.HasFile)
            {
                String fileName = fuFileUrl.PostedFile.FileName;
                String fileType = Path.GetExtension(fileName);
                String newFileName = DateTime.Now.Ticks.ToString() + fileType;
                fuFileUrl.PostedFile.SaveAs(Server.MapPath("~/") + "//ImageDatas//Files//" + newFileName);
                theFile.FileUrl = newFileName;
            }
            else
            {
                theFile.FileUrl = this.txtFileUrl.Text.Trim();
            }

            String action = ViewState["action"].ToString();

            if (action == HangSo.ACTION_ADD)
            {
                theFile.FileID = -1;
                fileIDOut = FileProvider.IUFile(theFile, true);
            }
            else
            {
                theFile.FileID = Int32.Parse(ViewState["FileID"].ToString());
                fileIDOut = FileProvider.IUFile(theFile, false);
            }

            Response.Redirect("AdminFileIU.aspx?FileID=" + fileIDOut.ToString() + "&Message=Lưu thành công");
        }
    }
}
